/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.storage.core.pool;

import java.io.InputStream;
import java.io.OutputStream;

import com.enfry.storage.core.DownloadStream;
import com.enfry.storage.core.UploadStream;
import com.enfry.storage.core.common.NameValuePair;
import com.enfry.storage.core.pool.FastdfsPool;
import com.enfry.storage.core.pool.StorageClient;

/**
 * Fastdfs操作封装
 * @Title com.enfry.storage.core.pool.Fastdfs
 * @author liulei
 * @date 2016年7月25日
 */
public class Fastdfs {
    private FastdfsPool fastdfsPool;

    public String uploadFile(String groupName, long fileSize, InputStream inputStream, String fileExtName, NameValuePair[] metaList) throws Exception {
        StorageClient client = fastdfsPool.getResource();
        UploadStream callback = new UploadStream(inputStream, fileSize);
        String result = client.upload_file1(groupName, fileSize, callback, fileExtName, metaList);
        fastdfsPool.returnResource(client);
        return result;
    }

    public String uploadFile(byte[] fileBuff, String fileExtName, NameValuePair[] metaList) throws Exception {
        StorageClient client = fastdfsPool.getResource();
        String result = client.upload_file1(fileBuff, fileExtName, metaList);
        fastdfsPool.returnResource(client);
        return result;
    }

    public String uploadFile(String masterFileId, String prefixName, byte[] fileBuff, String fileExtName, NameValuePair[] metaList) throws Exception {
        StorageClient client = fastdfsPool.getResource();
        String result = client.upload_file1(masterFileId, prefixName, fileBuff, fileExtName, metaList);
        fastdfsPool.returnResource(client);
        return result;
    }

    public byte[] downloadFile(String fileId) throws Exception {
        StorageClient client = fastdfsPool.getResource();
        byte[] result = client.download_file1(fileId);
        fastdfsPool.returnResource(client);
        return result;
    }

    public int downloadFile(String fileId, OutputStream outputStream) throws Exception {
        DownloadStream downStream = new DownloadStream(outputStream);
        StorageClient client = fastdfsPool.getResource();
        int result = client.download_file1(fileId, downStream);
        fastdfsPool.returnResource(client);
        return result;
    }

    public int deleteFile(String fileId) throws Exception {
        StorageClient client = fastdfsPool.getResource();
        int result = client.delete_file1(fileId);
        fastdfsPool.returnResource(client);
        return result;
    }

    public void setFastdfsPool(FastdfsPool fastdfsPool) {
        this.fastdfsPool = fastdfsPool;
    }
}
